package com.ruoyi.web.controller.publish;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.publish.domain.IdIdentsample;
import com.ruoyi.publish.domain.IdLaw;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.publish.domain.IdLegal;
import com.ruoyi.publish.service.IIdLegalService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 法律条文Controller
 * 
 * @author 张泽望
 * @date 2020-11-02
 */
@Controller
@RequestMapping("/publish/provisions")
public class IdLegalController extends BaseController
{
    private String prefix = "publish/provisions";

    @Autowired
    private IIdLegalService idLegalService;

//    @RequiresPermissions("publish:provisions:view")
    @GetMapping()
    public String provisions()
    {
        return prefix + "/provisions";
    }

    /**
     * 查询法律条文列表
     */
//    @RequiresPermissions("publish:provisions:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(IdLegal idLegal)
    {
        startPage();
        List<IdLegal> list = idLegalService.selectIdLegalList(idLegal);
        return getDataTable(list);
    }

    /**
     * 导出法律条文列表
     */
//    @RequiresPermissions("publish:provisions:export")
    @Log(title = "法律条文", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(IdLegal idLegal)
    {
        List<IdLegal> list = idLegalService.selectIdLegalList(idLegal);
        ExcelUtil<IdLegal> util = new ExcelUtil<IdLegal>(IdLegal.class);
        return util.exportExcel(list, "provisions");
    }




 //   @Log(title = "法律条文excel导入", businessType = BusinessType.IMPORT)
//    @PostMapping("/importData")
//    @ResponseBody
//    public AjaxResult importData(MultipartFile file, boolean updateSupport, Long registerId) throws Exception
//    {
//        if (registerId==null){
//            return  error("案件记录不能为空");
//        }
//        byte[] bytes = file.getBytes();
//        InputStream in = new ByteArrayInputStream(bytes);
//
//        ExcelUtil<IdLegal> util = new ExcelUtil<IdLegal>(IdIdentsample.class);
//        List<IdLegal> idLegalList = util.importExcel(in);
//
//        System.out.println("sieze="+idLegalList.size());
//        String operName = ShiroUtils.getSysUser().getLoginName();
//        String message = idIdentsampleService.importIdIdentsample(idIdentsampleList, updateSupport, operName, "2020",registerId);
//        return AjaxResult.success(message);
//    }


    //    @RequiresPermissions("system:user:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {

        ExcelUtil<IdLegal> util = new ExcelUtil<IdLegal>(IdLegal.class);
        List<IdLegal> list = new ArrayList<>();
        IdLegal idLegal = new IdLegal();
        //标题
        idLegal.setTitle("中华人民共和国著作权法(示范)");
        //法律条文
        idLegal.setLegalContent("法人或者非法人单位的作品,不论是否发表,依照本法享有著作权。 外国人的作品首先在中国境内发表的,依照本法享有著作权(示范)");
        list.add(idLegal);
        return util.importDataTemplateExcel(list,"法律条文");
    }










    /**
     * 新增法律条文
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存法律条文
     */
//    @RequiresPermissions("publish:provisions:add")
    @Log(title = "法律条文", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(IdLegal idLegal)
    {
        idLegal.setCreateName(ShiroUtils.getLoginName());
        idLegal.setCreateDate(DateUtils.getNowDate());
        idLegal.setBy1(ShiroUtils.getSysUser().getUserName());

        return toAjax(idLegalService.insertIdLegal(idLegal));
    }

    /**
     * 修改法律条文
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        IdLegal idLegal = idLegalService.selectIdLegalById(id);
        mmap.put("idLegal", idLegal);
        return prefix + "/edit";
    }

    /**
     * 修改保存法律条文
     */
//    @RequiresPermissions("publish:provisions:edit")
    @Log(title = "法律条文", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(IdLegal idLegal)
    {
        return toAjax(idLegalService.updateIdLegal(idLegal));
    }

    /**
     * 删除法律条文
     */
//    @RequiresPermissions("publish:provisions:remove")
    @Log(title = "法律条文", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(idLegalService.deleteIdLegalByIds(ids));
    }

    /**
     *  关联按键选择 单选
     */
    @GetMapping("/provisionsRadio")
    public String checkRadio()
    {
        return prefix + "/provisionsRadio";
    }


    @Log(title = "法律条文excel导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport, String year) throws Exception
    {

        byte[] bytes = file.getBytes();
        InputStream in = new ByteArrayInputStream(bytes);

        ExcelUtil<IdLegal> util = new ExcelUtil<IdLegal>(IdLegal.class);
        List<IdLegal> userList = util.importExcel(in);
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = idLegalService.importIdLegal(userList, updateSupport, operName, year);
        return AjaxResult.success(message);
    }
}
